package com.example.data.controller;

import com.example.data.entity.DataDic;
import com.example.data.service.DataDicService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/")
public class QueryDataController {

    @Resource(name = "DataDicServiceImpl")
    private DataDicService dataDicService;

    //查询全部
    @RequestMapping("/dataDic")
    public String dataDictionary(@RequestParam(value="pageNum",defaultValue="1")int pageNum,
            @RequestParam(value="pageSize",defaultValue="5")int pageSize,
            @ModelAttribute DataDic dataDic, HttpServletRequest request){

        //pagehelper 插件分页
        // pageNum 	当前页码   pageSize 	当前页面显示的数据条目
        //分页逻辑必须写在 service 层
        PageInfo<DataDic> pageInfo =  dataDicService.queryDataAll(pageNum,pageSize);

       request.setAttribute("pageInfo",pageInfo);
        return "data/dataDictionary";
    }

    // 条件查询
    @RequestMapping("/queryData")
    public String queryData( @ModelAttribute DataDic dataDic, HttpServletRequest request,
             @RequestParam(value="pageNum",defaultValue="1")int pageNum,
             @RequestParam(value="pageSize",defaultValue="5")int pageSize){


        PageInfo<DataDic> pageInfo =  dataDicService.queryData(dataDic,pageNum,pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "data/dataDictionary";
    }

    @RequestMapping("/saveData")
    public String saveData(DataDic dataDic,
                           @RequestParam(value="pageNum",defaultValue="1")int pageNum,
                           @RequestParam(value="pageSize",defaultValue="5")int pageSize,
                           HttpServletRequest request){

        String data_no = dataDic.getData_no();
        if(null==data_no){
            dataDic.setData_no(null);
        }
        dataDicService.saveData(dataDic);
        return "redirect:queryData";
    }


    @RequestMapping("/upd_data")
    public String upd_data(DataDic dataDic, String type,
                           @RequestParam(value="pageNum",defaultValue="1")int pageNum,
                           @RequestParam(value="pageSize",defaultValue="5")int pageSize,
                           HttpServletRequest request){

        String id = dataDic.getId();
        String cmd = request.getParameter("type");
        String result = "";
        if("query".equals(cmd)){
            PageInfo<DataDic> pageInfo =  dataDicService.queryData(dataDic,pageNum,pageSize);
            request.setAttribute("pageInfo",pageInfo);
            result = "/data/editTitle";
        }else if("modify".equals(cmd)){
            dataDicService.modify(dataDic);
            result = "redirect:dataDic";
        }

        return result;
    }

    @RequestMapping("/del_data")
    public String delete(DataDic dataDic,
                           @RequestParam(value="pageNum",defaultValue="1")int pageNum,
                           @RequestParam(value="pageSize",defaultValue="5")int pageSize,
                           HttpServletRequest request){

        dataDicService.delete(dataDic);
        return "redirect:queryData?pageNum="+pageNum;
    }

}
